<?php

class StatuspegawaiController extends Zend_Controller_Action
{   
    private $_status = null;
    public function preDispatch() {
        $this->_status = new Application_Model_Statuspegawai;
    }
    
    public function init()
    {
        if(!Zend_Auth::getInstance()->hasIdentity())
            $this->_redirect('/auth/');
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
        $status = $this->_status->getAllData();
        $row = $this->_status->checkRowstatuspegawai();
        $this->view->status = $status;
        if ($row == 0)
          $this->view->notFound = '<div class="notfound">Tidak Ada Data di Halaman Ini</div>';
        else
          $this->view->notFound = '<div class="notfound">Terdapat '.$row.' Data Pada Tabel Ini</div>';
    }

    public function addAction()
    {
        $form = new Application_Form_StatusPegawaiForm;
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            if($form->isValid($this->_request->getPost())) {
                $nama = $form->getValue('status_pegawai');
                $keterangan = $form->getValue('keterangan_status_pegawai');
                if($this->_status->checkstatuspegawai($nama))
                    $this->view->duplicate = '<ul class="errors" style="margin-left:150px;"><li>Status Sudah Ada</li></ul>';
                else {
                    $this->_status->statuspegawaiBaru($nama, $keterangan);
                    $this->_redirect("statuspegawai/index");                    
                }} else {
                    $form->populate($this->_request->getPost());
                }
        }
    }

    public function editAction()
    {
        $form = new Application_Form_StatusPegawaiForm;
        $form->tambah->setLabel('Simpan');
        
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            if($form->isValid($this->_request->getPost())) {
                $id = $form->getValue('id_status_pegawai');
                $nama = $form->getValue('status_pegawai');
                $keterangan = $form->getValue('keterangan_status_pegawai');
                
                $this->_status->statuspegawaiUpdate($id, $nama, $keterangan);
                
                $this->_redirect("statuspegawai/index");
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $form->populate($this->_status->getstatuspegawai($id));
            }
        }
    }

    public function deleteAction()
    {
        $form = new Application_Form_DeleteForm;
        
        $form->removeElement('id_departemen');
        $form->addElement('hidden', 'id_status_pegawai', 'int');
        
        $form->setAction("statuspegawai/delete");
        
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del1');
            if($del == 'Ya')
            {
                $this->_status->statuspegawaiDelete($this->getRequest()->getPost('id_status_pegawai'));
                $this->_redirect("statuspegawai/index");
            }
            else {
                $this->_redirect("statuspegawai/index");
            }
        } else {
           $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $form->populate($this->_status->getstatuspegawai($id));
            }
        }
    }


}







